import { Component, OnInit, ViewChild } from '@angular/core';
import { cloneDeep } from 'lodash';

@Component({
  selector: 'app-demo7',
  templateUrl: './demo7.component.html',
  styles: []
})
export class Demo7Component implements OnInit {
  loading = false;
  selected = [];
  _cacheSelected = [];

  @ViewChild('popover', { static: false}) popover;

  constructor() {}

  ngOnInit() {}

  beforeOpen = () =>
    new Promise(resolve => {
      setTimeout(() => {
        this._cacheSelected = cloneDeep(this.selected);
        resolve(true);
      }, 200);
    });

  externalClose() {
    this.selected = this._cacheSelected;
  }

  confirm() {
    this.loading = true;

    setTimeout(() => {
      this.loading = false;
      this._cacheSelected = cloneDeep(this.selected);
      this.popover.hide();
    }, 2000);
  }

  cancel() {
    this.selected = this._cacheSelected;
    this.popover.hide();
  }
}
